package com.awenhui.demo.extension;

import org.springframework.core.MethodParameter;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author heliuhui
 * @version 1.0
 * @date 2021/1/28 10:03
 */
public class SignInterceptor implements HandlerInterceptor {

    //方法执行之前执行
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //获取方法信息
        HandlerMethod handlerMethod = (HandlerMethod) handler;
        if (handlerMethod != null) {
            RequestMapping methodAnnotation = handlerMethod.getMethodAnnotation(RequestMapping.class);
            System.out.println(methodAnnotation.method() + ":" + methodAnnotation.value());
            //获取方法参数信息
            MethodParameter[] methodParameters = handlerMethod.getMethodParameters();
        }
        System.out.println("HandlerMethod==null");
        return true;

    }
    //方法执行完成，视图渲染之前执行
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }
    //视图渲染之后执行
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
